<!DOCTYPE html> 
<html>
  <head>
    <!--META-->
	<meta charset="utf-8">

	<title>Scan Live</title>
    <link href="css/mycss.css" type="text/css" rel="stylesheet">
    <script language="javascript" type="text/javascript">
///////////////////
//BLOCK DECLARATION
///////////////////
var idFirstLineScanTocs = 469;

//Block config DISE
var strBaliseScan = "javascript:PopUp('/AutoDiseWeb/automate/popupModule.jsp?modType=SIG_SIG&modCode=";
var strBaliseScan_arch = "er=0 src=/AutoDiseWeb/images/archive_logo.gif alt='sig. archivée'><a href=\"javas";
var strUrlInventaireAutomate = "http://10.197.68.30:4000/AutoDiseWeb/automate/index.jsp?viewTarget=objectListeView&page=";
var strDomaine = "127.0.0.1/ODA_DISE";
//var strDomaine = "10.85.32.5/ODA_DISE";

///////////////////
//BLOCK FUNC METIER
///////////////////
function init() {
    try {
        initLastScan();
    }
    catch (er) {
        log(0, "ERROR(init):" + er.message);
    }
}

function scanAutomate() {
    try {
		
		log(0, "INFO(scanAutomateLive): Début du scan");
		
		var strDateTime = getStrDateTime();
		setParameterL("init_last_scan_inventaire_live", strDateTime);

        viderInvTocsLive();
      
        for (var j = 1; j <= 5; j++) {
		
            urlToScan = strUrlInventaireAutomate + j + "&modType=SIG_SIG&milis="+getMilise();
			
			if (!(isThere(urlToScan))){
				break;
			}
			
            xhr_object = new AJ();
            xhr_object.open("GET", urlToScan, false);
            xhr_object.send(null);
            sourceHtml = xhr_object.responseText;
            tabSourceHtml = sourceHtml.split("\n");
            sizeTabSourceHtml = tabSourceHtml.length - 2;

            var i = idFirstLineScanTocs;
            
            while (i < sizeTabSourceHtml) {
                strLine = tabSourceHtml[i];
                while (((strLine.indexOf(strBaliseScan) == -1) & (strLine.indexOf(strBaliseScan_arch) == -1)) & (i < sizeTabSourceHtml)) {
                    i++;
                    strLine = tabSourceHtml[i];
                }
                if(i == sizeTabSourceHtml){
                    break;
                }

                //idToc
                strLine = tabSourceHtml[i];
                strIdToc = recuperer(strLine);
                
                //resp
                i = i + 2;
                strLine = tabSourceHtml[i];
                strResp = recuperer(strLine);

                //date creation
                i = i + 1;
                strLine = tabSourceHtml[i];
                strDateCreat = recuperer(strLine);
                
                //statut
                i = i + 1;
                strLine = tabSourceHtml[i];
                strStatut = recuperer(strLine);
                
                //dateStatut
                i = i + 1;
                strLine = tabSourceHtml[i];
                strDateStatut = recuperer(strLine);

                //liaisons
                i = i + 1;
                strLine = tabSourceHtml[i];
                strliaisons = "";

                while (strLine.indexOf("</table></td>") == -1) {
                    i += 1;
                    strLine = tabSourceHtml[i];
                    strRecup = recuperer(strLine);
                    reg = new RegExp("[a-zA-Z0-9]", "g");
                    tabStrRecup = strRecup.match(reg);
                    if ((tabStrRecup != null) & (strRecup != "&nbsp;")) {
                        strliaisons += "$" + strRecup;
                    }
                }
                
                //priorite
                strG = tabSourceHtml[i + 1].substring(22, 23);
                while (strG != "P") {
                    i++;
                    strG = tabSourceHtml[i + 1].substring(22, 23);
                }
                i++;
                strLine = tabSourceHtml[i];
                strPriorite = recuperer(strLine);

                //Emis
                strLine = tabSourceHtml[i - 1];
                strEmis = recuperer(strLine);
                
                //libelle
                i = i + 1;
                strLine = tabSourceHtml[i];
                strFinLigne = strLine.substring(strLine.length - 6, strLine.length - 1);
                strTagFinLigne = "</td>"
                if (strFinLigne == strTagFinLigne) {
                    strLibelle = recuperer(strLine);
                } else {
                    strLibelle = strLine.substring(22, strLine.length - 1);
                    strG = tabSourceHtml[i + 1].substring(1, 22);
                    while ((strG != "<td class=\"lineTabB\">") & (strG != "<td class=\"lineTabA\">")) {
                        i++;
                        strLine = tabSourceHtml[i];
                        strLibelle = strLibelle + strLine.substring(1, strLine.length - 6);
                        strG = tabSourceHtml[i + 1].substring(1, 22);
                    }
                }
                var reg = /[^éèàêa-zA-Z0-9_\/-]/;
                strLibelle = strLibelle.replace(reg, " ");

                //origine
                i = i + 1;
                strLine = tabSourceHtml[i];
                strOrigine = recuperer(strLine);
                
                //Application
                i = i + 1;
                strLine = tabSourceHtml[i];
                strApplication = recuperer(strLine);

                //PF détection
                i = i + 1;
                strLine = tabSourceHtml[i];
                strDetection = recuperer(strLine);

                //Duree
                i = i + 1;
                strLine = tabSourceHtml[i];
                strDuree = recuperer(strLine);
                if ((strDuree == 'ERROR') || (strDuree == '')) {
                    strDuree = tabSourceHtml[i+1];
                }

                insertTocsBaseLive(strIdToc, strResp, strDateCreat, strStatut, strDateStatut, strliaisons, strPriorite, strLibelle, strOrigine, strApplication, strDetection, strDuree, strEmis);
            }
        }
		
		var strDateTime = getStrDateTime();
        setParameterL("last_scan_inventaire_live", strDateTime);
		
		log(0, "INFO(scanAutomateLive): Fin du scan");
		
		initLastScan();
    }
    catch (er) {
        log(0, "ERROR(scanAutomateLive):" + er.message);
    } finally {
        delete xhr_object;
    }
}

///////////////////
//BLOCK FUNC UTILS
///////////////////
function log(type, msg) {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_insertLog.php?milis="+getMilise()+"&type=" + type + "&msg=" + msg, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
    } finally {
        delete xhr_object;
    }
}

function AJ() {
    try {
        var obj;
        if (window.XMLHttpRequest) obj = new XMLHttpRequest();
        else if (window.ActiveXObject) {
            try {
                obj = new ActiveXObject('MSXML2.XMLHTTP.3.0');
            }
            catch (er) {
                try {
                    obj = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (er) {
                    obj = false;
                }
            }
        }
        return obj;
    }
    catch (er) {
        log(0, "ERROR(AJ):" + er.message);
        return null;
    } 
}

function viderInvTocsLive() {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_truncateInvTocsLive.php?milis="+getMilise()+"&crtl=ok", false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
        log(0, "ERROR(viderInvTocsLive):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function insertTocsBaseLive(strIdToc, strResp, strDateCreat, strStatut, strDateStatut, strliaisons, strPriorite, strLibelle, strOrigine, strApplication, strDetection, strDuree, strEmis) {
    try {
        var tabParam = new Array();
        var strParam = "";
        var xhr_object = new AJ();
        var strReturn = "";

        tabParam["id_toc"] = strIdToc;
        tabParam["responsable"] = strResp;
        tabParam["date_creation"] = strDateCreat;
        tabParam["statut"] = strStatut;
        tabParam["date_statut"] = strDateStatut;
        tabParam["liaisons"] = strliaisons;
        tabParam["priorite"] = strPriorite;
        tabParam["libelle"] = strLibelle;
        tabParam["origine"] = strOrigine;
        tabParam["application"] = strApplication;
        tabParam["detection"] = strDetection;
        tabParam["duree"] = strDuree;
        tabParam["emis"] = strEmis;

        for (var indice in tabParam) {
            strParam += indice + "=" + tabParam[indice] + "&";
        }

        strParam = strParam.substring(0, strParam.length - 1);

        xhr_object.open("POST", "http://"+strDomaine+"/phpsql/mysql_insertTocsLive.php", false);
        xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr_object.send(strParam);
        if (xhr_object.readyState == 4) {
            strReturn = xhr_object.responseText;
        } else {
            strReturn = "ERROR";
        }

        if (strReturn.length > 4) {
            log(0, "ERROR(insertTocsBase) : "+ strReturn);
        }
    }
    catch (er) {
        log(0, "ERROR(insertTocsBase):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function recuperer(p_str) {
    var str = new String(p_str);
    if (str.length > 1) {
        var resultat = "none";
        try {
            //remplacer tous les tab
            reg = new RegExp("[\t]", "g")
            str = str.replace(reg, '');

            var reg = /(>)*[^<^>]+(<|$)/;
            resultat = str.match(reg);
            resultat = resultat[0];
            resultat = resultat.substring(1, resultat.length - 1);
            reg = /[^éèàêa-zA-Z0-9_\/-]/;
            resultat = resultat.replace(reg, " ");

            if (resultat == "mg") {
                resultat = "";
            }
        }
        catch (er) {
            resultat = "";
        }

        if ((str.indexOf("></td>") != -1)&(resultat == "")) {
            resultat = "&nbsp;";
        }
        
        return resultat;
    } else {
        return str;
    }
}

function getParameterL(p_param_name) {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_getParamL.php?milis="+getMilise()+"&param_name=" + p_param_name, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        if (strResponse != "ERROR") {
            tabResponse = strResponse.split("$");
        }

        type = tabResponse[0];

        switch (type) {
            case "int":
                return parseInt(tabResponse[1]);
                break;
            case "varchar":
                return tabResponse[1];
                break;
            default:
                return tabResponse[1];
                break;
        }

    }
    catch (er) {
        log(0, "ERROR(getParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function setParameterL(p_param_name, p_param_value) {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/API/phpsql/mysql_setParam.php?milis="+getMilise()+"&param_name=" + p_param_name + "&param_value=" + p_param_value, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
        log(0, "ERROR(setParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function getStrDateTime() {
    try {
        var currentTime = new Date();
        var hours = pad2(currentTime.getHours());
        var minutes = pad2(currentTime.getMinutes());
        var annee = currentTime.getFullYear();
        var mois = pad2(currentTime.getMonth()+1);
        var jour = pad2(currentTime.getDate());
        var secondes = pad2(currentTime.getSeconds());
        var strDateTime = annee + "/" + mois + "/" + jour + " " + hours + ":" + minutes + ":" + secondes;
        return strDateTime;
    }
    catch (er) {
        log(0, "ERROR(getStrDateTime):" + er.message);
        return null;
    }
}

function pad2(number) {
    try {
        return (number < 10 ? '0' : '') + number;
    }
    catch (er) {
        log(0, "ERROR(pad2):" + er.message);
        return null;
    }
}

function getMilise() {
    var d = new Date();
    return d.getTime();
}

function isThere(url) {
    try {
        var xhr_object = new AJ();
        xhr_object.open("GET", url, false);
        xhr_object.send(null);

        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        responseSize = strResponse.length;

        if (responseSize > 16500) {
            return true;
        } else {
            return false;
        }

        return boolStatus;
    }
    catch (er) {
        log(0, "ERROR(isThere):" + er.message);
        return false;
    } finally {
        delete xhr_object;
    }
}

function initLastScan() {
    try {
        var labLastScanTxt = "";

        labLastScanTxt = "<i>";

        var labDate = getParameterL("last_scan_inventaire_live");

        labLastScanTxt += "Dernier scan : " + labDate + "</br>";
        labLastScanTxt += "</i>";

        labLastScan.innerHTML = labLastScanTxt;
    }
    catch (er) {
        log(0, "ERROR(initLastScan):" + er.message);
    }
}

</script>
  </head>
  <body onload="init();">
    <table style="text-align: left; width: 100%; height: 100%;" border="0"cellpadding="0" cellspacing="0">
      <tbody>
        <tr>
          <td style="height: 100px; background-color: rgb(204, 204, 204);">
            <table style="text-align: left; width: 100%; height: 100%;" border="0" cellpadding="0" cellspacing="0">
              <tbody>
                <tr>
                  <td style="width: 10px;"></td>
                  <td style="text-align: center; width: 200px;">
                    <img src="img/logo_CGI.gif" />
                  </td>
                  <td></td>
                  <td style="width: 200px;"></td>
                  <td style="width: 10px;"></td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
        <tr>
          <td style="background-color:#E31937; height: 2px;"></td>
        </tr>
        <tr align="center" valign="top">
          <td>
              <table cellpadding="0" cellspacing="0" style="text-align: left; width: 100%; height: 100%;">
                  <tr>
                      <td style="width: 10px;">
                          &nbsp;</td>
                      <td>
                          &nbsp;</td>
                      <td style="width: 10px;">
                          &nbsp;</td>
                  </tr>
                  <tr>
                      <td style="width: 10px;">&nbsp;</td>
                      <td style="text-align: center">&nbsp;</td>
                      <td style="width: 10px;">&nbsp;</td>
                  </tr>
                  <tr>
                      <td style="width: 10px;"></td>
                      <td style="height: 2px; background-color: rgb(204, 204, 204);"></td>
                      <td style="width: 10px;"></td>
                  </tr>
                  <tr>
                      <td style="width: 10px;">
                          &nbsp;</td>
                      <td>
                      <center>
                      <FORM method="post" id="formCreerDossier">
						<br />
						<a href="javascript:;"onClick="scanAutomate();" id="A1">Lancer le scan Automate</a>
						<br />
						<div id="labLastScan" style="margin: 0 auto"></div>
						<br />
						</FORM>
                      </center>   
                      </td>
                      <td style="width: 10px;">
                          &nbsp;</td>
                  </tr>
                  <tr>
                      <td style="width: 10px;">
                          &nbsp;</td>
                      <td>
                          &nbsp;</td>
                      <td style="width: 10px;">
                          &nbsp;</td>
                  </tr>
              </table>
            </td>
        </tr>
        <tr>
          <td style="background-color: silver; height: 2px;"></td>
        </tr>
        <tr>
          <td style="height: 100px;"></td>
        </tr>
      </tbody>
    </table>
  </body>
</html>